/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TDept;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TDeptDao;
import com.ruipengkj.service.TDeptService;
/**
 * 说明：
 * 公司部门Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TDeptServiceImpl implements TDeptService{
	@Resource
	private TDeptDao TDeptDao;//DAO层
	//查询所有
	@Override
	public List<TDept> findService(TDept entity) throws Exception {
		String hql = "select o from TDept o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(!"".equals(entity.getName())){
				whereHQL.append("Name=?").append(BaseQuery.AND);
				param.add(entity.getName());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TDeptDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	
	@Override
	public PageModel<TDept> getScrollService(int firstResult,
			int maxResult, TDept entity) throws Exception {
		String hql = "select o from TDept o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TDeptDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

	//修改
	@Override
	public void modifyService(TDept entity) throws Exception {
		TDeptDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TDept entity) throws Exception {
		TDeptDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TDeptDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TDept getByIdService(String id) throws Exception {
		return TDeptDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TDept entity) throws Exception {
		TDeptDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TDept entity) throws Exception {
		return TDeptDao.saveRetDao(entity);
	}
	public TDeptDao getTDeptDao() {
		return TDeptDao;
	}

	public void setTDeptDao(TDeptDao TDeptDao) {
		this.TDeptDao = TDeptDao;
	}

	@Override
	public TDept getTDeptByDeptName(String deptName) throws Exception {
		String hql = "select o from TDept o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		whereHQL.append("name=?");
		param.add(deptName);
		List<TDept> list=TDeptDao.findDao(hql, whereHQL.toString(), param, null);
		if(list!=null&&list.size()!=0){
			return list.get(0);
		}
		return null;
	}
}